package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Inventory;
import com.restaurant.service.InventoryService;
import com.restaurant.validators.InventoryDeleteFormValidator;
import com.restaurant.validators.InventoryFormValidator;

@Component
@RequestMapping("/action/inventory.do")
@SessionAttributes("inventory")
public class InventoryFormcontroller {

	@Autowired
	private InventoryService objInventoryService;
	
	@Autowired
	private InventoryDeleteFormValidator inventoryDeleteFormValidator;
	
	@Autowired
	private InventoryFormValidator inventoryFormValidator;
	
	@RequestMapping(method=RequestMethod.GET)
	public String inventoryGETRequest(ModelMap map){
		Inventory inventory = new Inventory();
		ArrayList<Inventory> listInventories = null;
		
		try {
			listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where isdeleted = false");
		} catch (Exception e) {
			e.printStackTrace();
		}	
		
		map.addAttribute("inventory", inventory);
		map.addAttribute("listInventories", listInventories);
		return "Inventory";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String inventoryPOSTRequest(ModelMap map, @ModelAttribute Inventory inventory, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		ArrayList<Inventory> listInventories = null;
		
		try{
			if(action.equals("addnew")){
				inventoryFormValidator.validate(inventory, result);
				if(result.hasErrors()){
					try {
						listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where isdeleted = false");
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listInventories", listInventories);
					return "Inventory";
				}
				inventory.setIsdeleted(false);
				objInventoryService.addOrModify(inventory);
				inventory = new Inventory();
			}else if(action.equals("edit")){
				inventory = objInventoryService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				objInventoryService.addOrModify(inventory);
			}else if(action.equals("del")){
				inventory = objInventoryService.loadByPrimaryKey(Long.parseLong(request.getParameter("id")));
				inventoryDeleteFormValidator.validate(inventory, result);
				if(result.hasErrors()){
					try {
						listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where isdeleted = false");
					} catch (Exception e) {
						e.printStackTrace();
					}
					map.addAttribute("listInventories", listInventories);
					return "Inventory";
				}
				inventory.setIsdeleted(true);
				objInventoryService.addOrModify(inventory);
				inventory = new Inventory();
			}else{
				return "redirect:inventory.do";
			}
			
		}catch(Exception e){
			return "redirect:inventory.do";
		}
		
		try {
			listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where isdeleted=false");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		map.addAttribute("inventory", inventory);
		map.addAttribute("listInventories", listInventories);
		return "Inventory";
	}
}
